﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    public partial class CheckIfoForm : Form
    {
        private UserService userService = new UserService();
        private List<User> usersList = new List<User>();
        private List<Order> ordersList = new List<Order>();
        private OrderService orderService = new OrderService();
        private StationService stationService = new StationService();
        private CarriageService carriageService = new CarriageService();
        private TrainSequenceService trainSequenceService = new TrainSequenceService();
        public static CheckIfoForm checkIfoFormFrom;
        private User currentUser;
        public CheckIfoForm()
        {
            InitializeComponent();
            checkIfoFormFrom = this;
            currentUser = userService.FindUserByAccount(LoginFrom.userAccount);
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CheckIfoForm_HelpRequested(object sender, HelpEventArgs hlpevent)
        {
            UserForm.userForm.Show();
        }

        private void CheckIfoForm_Load(object sender, EventArgs e)
        {
            ShowUserInformation();
            ShowOrderInformation();
        }

        private void ShowOrderInformation()
        {
            ordersList = orderService.FindOrderByAccount(currentUser.Account);
            if(ordersList.Count == 0)
            {
                return;
            }
            dataTable.RowCount = ordersList.Count;
            for(int i = 0; i < ordersList.Count; i++)
            {
                TrainSequence ts = trainSequenceService.FindTrainSequenceById(ordersList[i].TrainSequenceId);
                dataTable[0,i].Value = ts.TrainNum;
                dataTable[1,i].Value = ts.TrainId;
                dataTable[2,i].Value = stationService.FindStationNameById(ts.StartStation);
                dataTable[3,i].Value = stationService.FindStationNameById(ts.EndStation);
                dataTable[4,i].Value = ts.LaunchTime.ToString();
                dataTable[5,i].Value = ts.ArriveTime.ToString();
                dataTable[6,i].Value = ts.TravelTime.ToString();
                dataTable[7,i].Value = ordersList[i].SeatId;
                dataTable[8,i].Value = carriageService.FindPriceBySeatId(ordersList[i].SeatId);
            }
        }

        private void ShowUserInformation()
        {
            textBoxAccount.Text = currentUser.Account;
            textBoxUserName.Text = currentUser.Name;
            textBoxPhoneNum.Text = currentUser.Phonenum;
            textBoxSex.Text = currentUser.Sex;
            textBoxCertificate_num.Text = currentUser.CertificateNum;
        }

        private void CheckIfoForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            UserForm.userForm.Show();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            
        }
    }
}
